import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:gt_flutter_admin/constant/error_msg.dart';
import 'package:gt_flutter_admin/pages/system/role/role_update_controller.dart';

import '../../../utils/data_format_utils.dart';

class RoleUpdateView extends GetView<RoleUpdateController> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Form(
        child: Column(
          children: [
            FormField(
              builder: (field) => Obx(
                () => BrnTextInputFormItem(
                  controller: controller.roleNameText,
                  title: '角色名称',
                  hint: '请输入角色名称',
                  isRequire: true,
                  isEdit: controller.isEdit.value,
                ),
              ),
              validator: (value) =>
                  ErrorMsgs.validNull(controller.roleNameText),
            ),
            FormField(
              builder: (field) => Obx(
                () => BrnTextSelectFormItem(
                  title: '菜单',
                  hint: '请选择菜单',
                  value: controller.menuNames.value,
                  isRequire: true,
                  onTap: () {
                    // 清空旧选项
                    controller.menuIds.clear();
                    controller.menuNames.value = '';

                    BrnPickerEntity topMenu =
                        DataFormatUtils.brnPickerEntityMap(
                          controller.menuPage.value.rows!,
                          PickerFilterType.checkbox,
                        );

                    _buildRangePicker(topMenu);
                  },
                ),
              ),
            ),
            BrnTextInputFormItem(
              controller: controller.remarkText,
              title: '描述',
              hint: '请输入描述',
            ),
            Builder(
              builder: (context) {
                return BrnBigMainButton(
                  title: controller.isEdit.value ? '新增' : '编辑',
                  onTap: () {
                    if (Form.of(context).validate()) {
                      controller.onSubmit();
                    }
                  },
                );
              },
            ),
          ],
        ),
      ),
    );
  }

  // 构建级联选择
  void _buildRangePicker(BrnPickerEntity topMenu) {
    showModalBottomSheet(
      context: Get.context!,
      backgroundColor: Colors.transparent,
      builder: (BuildContext dialogContext) => BrnMultiColumnPicker(
        entity: topMenu,
        onEntityTap: (columnIndex, rowIndex, entity) {
          if (entity.isSelected &&
              !controller.menuNames.contains(entity.name)) {
            controller.menuIds.add(int.parse(entity.key!));
            controller.menuNames.value += '${entity.name} ';
          } else if (controller.menuNames.contains(entity.name)) {
            controller.menuIds.remove(int.parse(entity.key!));
            controller.menuNames.value = controller.menuNames.value.replaceAll(
              '${entity.name} ',
              '',
            );
          }
        },
        pickerTitleConfig: BrnPickerTitleConfig(
          cancel: Text(''),
          confirm: Text(''),
          titleContent: '选择菜单',
        ),
      ),
    );
  }
}
